/***************************************************************
Made by: Cultured::Labs

Copyright (c) 2010 Cultured::Labs
****************************************************************/
/////////////////////////////////////////////////////////////////
//Description:                                                 //
//A simple hangman game with file input and graphics.          //
/////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <algorithm>
#include "getWord.h"
#include "checkCharacter.h"
#include "printStickman.h"
using namespace std;

int main() {
  unsigned int discovered = 0;
  unsigned int attemps = 8;
  string nonceword;
  string misses;
  string guess;

  //gets a random word from the wordList.txt
  string secretword = getWord();
  if (secretword == "#error#") {
    cout << "Could not open word_list.txt!" << endl;
    cin.get();
    return 1;
  }

  //sets nonceword to '-' characters
  for (unsigned int i = 1; i <= secretword.size(); i++) {
    nonceword.append("-");
  }

  //game loop
  do {
    //prints gametable
    printStickman(attemps);
    cout << " You still have " << attemps << " attemps." << endl;
    cout << "**************************" << endl;
    cout << "* Word:   " << nonceword << "  (" << secretword.size() - discovered << ")" << endl;
    cout << "* Misses: " << misses << endl;
    cout << "* Guess: ";

    //get user input
    cin >> guess;
    transform(guess.begin(), guess.end(), guess.begin(), ::tolower);

    //checks whether the input character is valid
    if (guess.size() == 1) {
      int found = checkCharacter(guess[0], secretword, nonceword, misses);

      if (found != 0) {
        discovered += found;
      } else {
        attemps--;
      }
    }

    //checks for a complete word solution
    if (guess == secretword) {
      discovered = secretword.size();
      break;
    }
    else if (guess.size() != 1) {
      attemps--;
    }

    cout << endl << endl;
  } while (secretword.size() > discovered && attemps > 0);


  //prints upshot
  cout << endl;
  if (discovered == secretword.size()) {
    cout << "CORRECT!" << endl;
  } else {
    cout << "YOU LOSE!" << endl;
    printStickman(0); //prints complete hangman
  }
  cout << "The secret word was \"" << secretword << "\"." << endl << endl;
  cin.get();
  cin.get();
}
